{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e7c60269",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 定义算法\n",
    "def kennardstonealgorithm(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b229be4e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "番泻苷2:1\n",
      "训练集样本编号:\n",
      "['12-31' '3-22' '7-29' '3-18' '10-24' '5-1' '12-6' '13-31' '4-4' '11-13'\n",
      " '4-27' '7-18' '5-3' '1-1' '4-22' '6-21' '11-4' '5-31' '12-24' '5-16'\n",
      " '13-9' '6-1' '8-21' '2-22' '5-23' '12-2' '7-7' '4-24' '5-18' '13-4' '4-1'\n",
      " '6-4' '1-26' '12-26' '4-8' '1-30' '2-28' '2-3' '3-4' '1-4' '3-23' '10-12'\n",
      " '6-14' '4-31' '13-1' '11-3' '6-15' '12-7' '13-29' '10-15' '8-24' '10-1'\n",
      " '5-30' '9-1' '12-1' '5-9' '1-22' '7-1' '12-29' '9-13' '8-11' '8-31'\n",
      " '12-30' '2-1' '4-11' '11-18' '9-29' '8-27' '2-14' '3-31' '5-11' '10-31'\n",
      " '3-14' '5-20' '13-25' '7-4' '7-27' '5-12' '13-6' '4-15' '2-31' '4-2'\n",
      " '10-19' '7-3' '8-8' '8-22' '11-30' '8-28' '8-9' '9-9' '2-2' '4-25' '5-2'\n",
      " '2-19' '10-2' '1-14' '9-24' '4-23' '8-23' '13-27' '1-11' '5-22' '7-13'\n",
      " '10-4' '7-15' '6-10' '4-20' '6-27' '12-10' '11-2' '1-24' '10-28' '5-25'\n",
      " '9-3' '12-25' '13-16' '5-13' '3-10' '6-31' '3-11' '5-4' '12-28' '12-11'\n",
      " '8-15' '3-26' '3-17' '13-14' '5-24' '8-25' '6-6' '9-19' '6-19' '13-22'\n",
      " '1-19' '11-24' '3-3' '1-2' '4-9' '7-12' '8-19' '8-29' '10-10' '13-10'\n",
      " '9-21' '10-14' '9-26' '8-16' '8-12' '5-26' '7-26' '4-26' '4-21' '13-23'\n",
      " '11-1' '13-26' '8-17' '9-10' '13-2' '8-2' '2-13' '2-11' '6-9' '1-6'\n",
      " '1-16' '7-28' '3-20' '10-7' '8-5' '12-27' '10-16' '9-2' '2-5' '5-8'\n",
      " '13-12' '12-17' '6-26' '10-29' '10-8' '1-28' '13-28' '13-20' '11-14'\n",
      " '1-17' '11-10' '12-8' '8-26' '4-13' '4-3' '6-3' '7-6' '8-3' '10-22'\n",
      " '3-25' '7-20' '4-29' '7-2' '3-28' '9-12' '12-12' '13-24' '12-16' '11-28'\n",
      " '9-15' '1-10' '7-19' '2-6' '8-30' '9-30' '7-23' '5-14' '6-13' '6-2'\n",
      " '3-16' '1-25' '7-16' '11-22' '9-28' '6-28' '2-24' '1-31' '10-17' '11-16'\n",
      " '8-20' '2-30' '3-19' '12-23' '9-17' '1-5' '9-18' '1-8' '7-11' '4-16'\n",
      " '5-7' '10-21' '2-29' '3-15' '12-14' '12-4' '4-14' '8-7' '11-6' '6-30'\n",
      " '6-17' '3-13' '1-3' '13-11' '3-30' '11-23' '7-17' '8-18' '2-12' '9-4'\n",
      " '7-5' '13-15' '10-27' '4-6' '3-8' '10-30' '5-19' '8-6' '12-13' '6-5'\n",
      " '5-6' '10-18' '4-12' '10-9' '10-6' '7-9' '9-7']\n",
      "测试集样本编号:\n",
      "['1-7' '1-9' '1-12' '1-13' '1-15' '1-18' '1-20' '1-21' '1-23' '1-27'\n",
      " '1-29' '2-4' '2-7' '2-8' '2-9' '2-10' '2-15' '2-16' '2-17' '2-18' '2-20'\n",
      " '2-21' '2-23' '2-25' '2-26' '2-27' '3-1' '3-2' '3-5' '3-6' '3-7' '3-9'\n",
      " '3-12' '3-21' '3-24' '3-27' '3-29' '4-5' '4-7' '4-10' '4-17' '4-18'\n",
      " '4-19' '4-28' '4-30' '5-5' '5-10' '5-15' '5-17' '5-21' '5-27' '5-28'\n",
      " '5-29' '6-7' '6-8' '6-11' '6-12' '6-16' '6-18' '6-20' '6-22' '6-23'\n",
      " '6-24' '6-25' '6-29' '7-8' '7-10' '7-14' '7-21' '7-22' '7-24' '7-25'\n",
      " '7-30' '7-31' '8-1' '8-4' '8-10' '8-13' '8-14' '9-5' '9-6' '9-8' '9-11'\n",
      " '9-14' '9-16' '9-20' '9-22' '9-23' '9-25' '9-27' '9-31' '10-3' '10-5'\n",
      " '10-11' '10-13' '10-20' '10-23' '10-25' '10-26' '11-5' '11-7' '11-8'\n",
      " '11-9' '11-11' '11-12' '11-15' '11-17' '11-19' '11-20' '11-21' '11-25'\n",
      " '11-26' '11-27' '11-29' '11-31' '12-3' '12-5' '12-9' '12-15' '12-18'\n",
      " '12-19' '12-20' '12-21' '12-22' '13-3' '13-5' '13-7' '13-8' '13-13'\n",
      " '13-17' '13-18' '13-19' '13-21' '13-30']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 读取Excel文件，并假设第一列是样本编号，其余列是特征数据\n",
    "df = pd.read_excel(\"F:\\\\研究\\\\番泻苷在线提取数据.xlsx\", sheet_name='红外谱图')\n",
    "# 第一列是样本编号，其余是特征数据\n",
    "sample_ids = df.iloc[:, 0]  # 保存样本编号\n",
    "x_variables = df.iloc[:, 1:].values  # 转换特征数据为numpy数组\n",
    "\n",
    "# 确保x_variables只包含数值数据\n",
    "x_variables = np.array(x_variables, dtype=float)\n",
    "\n",
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 269)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"番泻苷2:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eefc0149",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "番泻苷3:1\n",
      "训练集样本编号:\n",
      "['12-31' '3-22' '7-29' '3-18' '10-24' '5-1' '12-6' '13-31' '4-4' '11-13'\n",
      " '4-27' '7-18' '5-3' '1-1' '4-22' '6-21' '11-4' '5-31' '12-24' '5-16'\n",
      " '13-9' '6-1' '8-21' '2-22' '5-23' '12-2' '7-7' '4-24' '5-18' '13-4' '4-1'\n",
      " '6-4' '1-26' '12-26' '4-8' '1-30' '2-28' '2-3' '3-4' '1-4' '3-23' '10-12'\n",
      " '6-14' '4-31' '13-1' '11-3' '6-15' '12-7' '13-29' '10-15' '8-24' '10-1'\n",
      " '5-30' '9-1' '12-1' '5-9' '1-22' '7-1' '12-29' '9-13' '8-11' '8-31'\n",
      " '12-30' '2-1' '4-11' '11-18' '9-29' '8-27' '2-14' '3-31' '5-11' '10-31'\n",
      " '3-14' '5-20' '13-25' '7-4' '7-27' '5-12' '13-6' '4-15' '2-31' '4-2'\n",
      " '10-19' '7-3' '8-8' '8-22' '11-30' '8-28' '8-9' '9-9' '2-2' '4-25' '5-2'\n",
      " '2-19' '10-2' '1-14' '9-24' '4-23' '8-23' '13-27' '1-11' '5-22' '7-13'\n",
      " '10-4' '7-15' '6-10' '4-20' '6-27' '12-10' '11-2' '1-24' '10-28' '5-25'\n",
      " '9-3' '12-25' '13-16' '5-13' '3-10' '6-31' '3-11' '5-4' '12-28' '12-11'\n",
      " '8-15' '3-26' '3-17' '13-14' '5-24' '8-25' '6-6' '9-19' '6-19' '13-22'\n",
      " '1-19' '11-24' '3-3' '1-2' '4-9' '7-12' '8-19' '8-29' '10-10' '13-10'\n",
      " '9-21' '10-14' '9-26' '8-16' '8-12' '5-26' '7-26' '4-26' '4-21' '13-23'\n",
      " '11-1' '13-26' '8-17' '9-10' '13-2' '8-2' '2-13' '2-11' '6-9' '1-6'\n",
      " '1-16' '7-28' '3-20' '10-7' '8-5' '12-27' '10-16' '9-2' '2-5' '5-8'\n",
      " '13-12' '12-17' '6-26' '10-29' '10-8' '1-28' '13-28' '13-20' '11-14'\n",
      " '1-17' '11-10' '12-8' '8-26' '4-13' '4-3' '6-3' '7-6' '8-3' '10-22'\n",
      " '3-25' '7-20' '4-29' '7-2' '3-28' '9-12' '12-12' '13-24' '12-16' '11-28'\n",
      " '9-15' '1-10' '7-19' '2-6' '8-30' '9-30' '7-23' '5-14' '6-13' '6-2'\n",
      " '3-16' '1-25' '7-16' '11-22' '9-28' '6-28' '2-24' '1-31' '10-17' '11-16'\n",
      " '8-20' '2-30' '3-19' '12-23' '9-17' '1-5' '9-18' '1-8' '7-11' '4-16'\n",
      " '5-7' '10-21' '2-29' '3-15' '12-14' '12-4' '4-14' '8-7' '11-6' '6-30'\n",
      " '6-17' '3-13' '1-3' '13-11' '3-30' '11-23' '7-17' '8-18' '2-12' '9-4'\n",
      " '7-5' '13-15' '10-27' '4-6' '3-8' '10-30' '5-19' '8-6' '12-13' '6-5'\n",
      " '5-6' '10-18' '4-12' '10-9' '10-6' '7-9' '9-7' '10-5' '5-17' '6-12'\n",
      " '4-30' '3-12' '12-3' '6-22' '7-22' '4-19' '1-12' '3-2' '4-28' '13-13'\n",
      " '8-14' '6-23' '1-20' '6-7' '11-29' '11-11' '7-24' '5-10' '8-1' '7-8'\n",
      " '7-31' '9-25' '7-25' '2-23' '11-9' '9-20' '5-27' '8-13' '6-18' '7-10']\n",
      "测试集样本编号:\n",
      "['1-7' '1-9' '1-13' '1-15' '1-18' '1-21' '1-23' '1-27' '1-29' '2-4' '2-7'\n",
      " '2-8' '2-9' '2-10' '2-15' '2-16' '2-17' '2-18' '2-20' '2-21' '2-25'\n",
      " '2-26' '2-27' '3-1' '3-5' '3-6' '3-7' '3-9' '3-21' '3-24' '3-27' '3-29'\n",
      " '4-5' '4-7' '4-10' '4-17' '4-18' '5-5' '5-15' '5-21' '5-28' '5-29' '6-8'\n",
      " '6-11' '6-16' '6-20' '6-24' '6-25' '6-29' '7-14' '7-21' '7-30' '8-4'\n",
      " '8-10' '9-5' '9-6' '9-8' '9-11' '9-14' '9-16' '9-22' '9-23' '9-27' '9-31'\n",
      " '10-3' '10-11' '10-13' '10-20' '10-23' '10-25' '10-26' '11-5' '11-7'\n",
      " '11-8' '11-12' '11-15' '11-17' '11-19' '11-20' '11-21' '11-25' '11-26'\n",
      " '11-27' '11-31' '12-5' '12-9' '12-15' '12-18' '12-19' '12-20' '12-21'\n",
      " '12-22' '13-3' '13-5' '13-7' '13-8' '13-17' '13-18' '13-19' '13-21'\n",
      " '13-30']\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 302)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"番泻苷3:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "10c949ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "番泻苷4:1\n",
      "训练集样本编号:\n",
      "['12-31' '3-22' '7-29' '3-18' '10-24' '5-1' '12-6' '13-31' '4-4' '11-13'\n",
      " '4-27' '7-18' '5-3' '1-1' '4-22' '6-21' '11-4' '5-31' '12-24' '5-16'\n",
      " '13-9' '6-1' '8-21' '2-22' '5-23' '12-2' '7-7' '4-24' '5-18' '13-4' '4-1'\n",
      " '6-4' '1-26' '12-26' '4-8' '1-30' '2-28' '2-3' '3-4' '1-4' '3-23' '10-12'\n",
      " '6-14' '4-31' '13-1' '11-3' '6-15' '12-7' '13-29' '10-15' '8-24' '10-1'\n",
      " '5-30' '9-1' '12-1' '5-9' '1-22' '7-1' '12-29' '9-13' '8-11' '8-31'\n",
      " '12-30' '2-1' '4-11' '11-18' '9-29' '8-27' '2-14' '3-31' '5-11' '10-31'\n",
      " '3-14' '5-20' '13-25' '7-4' '7-27' '5-12' '13-6' '4-15' '2-31' '4-2'\n",
      " '10-19' '7-3' '8-8' '8-22' '11-30' '8-28' '8-9' '9-9' '2-2' '4-25' '5-2'\n",
      " '2-19' '10-2' '1-14' '9-24' '4-23' '8-23' '13-27' '1-11' '5-22' '7-13'\n",
      " '10-4' '7-15' '6-10' '4-20' '6-27' '12-10' '11-2' '1-24' '10-28' '5-25'\n",
      " '9-3' '12-25' '13-16' '5-13' '3-10' '6-31' '3-11' '5-4' '12-28' '12-11'\n",
      " '8-15' '3-26' '3-17' '13-14' '5-24' '8-25' '6-6' '9-19' '6-19' '13-22'\n",
      " '1-19' '11-24' '3-3' '1-2' '4-9' '7-12' '8-19' '8-29' '10-10' '13-10'\n",
      " '9-21' '10-14' '9-26' '8-16' '8-12' '5-26' '7-26' '4-26' '4-21' '13-23'\n",
      " '11-1' '13-26' '8-17' '9-10' '13-2' '8-2' '2-13' '2-11' '6-9' '1-6'\n",
      " '1-16' '7-28' '3-20' '10-7' '8-5' '12-27' '10-16' '9-2' '2-5' '5-8'\n",
      " '13-12' '12-17' '6-26' '10-29' '10-8' '1-28' '13-28' '13-20' '11-14'\n",
      " '1-17' '11-10' '12-8' '8-26' '4-13' '4-3' '6-3' '7-6' '8-3' '10-22'\n",
      " '3-25' '7-20' '4-29' '7-2' '3-28' '9-12' '12-12' '13-24' '12-16' '11-28'\n",
      " '9-15' '1-10' '7-19' '2-6' '8-30' '9-30' '7-23' '5-14' '6-13' '6-2'\n",
      " '3-16' '1-25' '7-16' '11-22' '9-28' '6-28' '2-24' '1-31' '10-17' '11-16'\n",
      " '8-20' '2-30' '3-19' '12-23' '9-17' '1-5' '9-18' '1-8' '7-11' '4-16'\n",
      " '5-7' '10-21' '2-29' '3-15' '12-14' '12-4' '4-14' '8-7' '11-6' '6-30'\n",
      " '6-17' '3-13' '1-3' '13-11' '3-30' '11-23' '7-17' '8-18' '2-12' '9-4'\n",
      " '7-5' '13-15' '10-27' '4-6' '3-8' '10-30' '5-19' '8-6' '12-13' '6-5'\n",
      " '5-6' '10-18' '4-12' '10-9' '10-6' '7-9' '9-7' '10-5' '5-17' '6-12'\n",
      " '4-30' '3-12' '12-3' '6-22' '7-22' '4-19' '1-12' '3-2' '4-28' '13-13'\n",
      " '8-14' '6-23' '1-20' '6-7' '11-29' '11-11' '7-24' '5-10' '8-1' '7-8'\n",
      " '7-31' '9-25' '7-25' '2-23' '11-9' '9-20' '5-27' '8-13' '6-18' '7-10'\n",
      " '9-11' '2-4' '3-9' '3-29' '3-21' '11-19' '6-11' '12-18' '1-23' '2-26'\n",
      " '13-5' '13-19' '3-1' '10-13' '6-29' '3-5' '11-26' '2-27' '11-5' '7-30']\n",
      "测试集样本编号:\n",
      "['1-7' '1-9' '1-13' '1-15' '1-18' '1-21' '1-27' '1-29' '2-7' '2-8' '2-9'\n",
      " '2-10' '2-15' '2-16' '2-17' '2-18' '2-20' '2-21' '2-25' '3-6' '3-7'\n",
      " '3-24' '3-27' '4-5' '4-7' '4-10' '4-17' '4-18' '5-5' '5-15' '5-21' '5-28'\n",
      " '5-29' '6-8' '6-16' '6-20' '6-24' '6-25' '7-14' '7-21' '8-4' '8-10' '9-5'\n",
      " '9-6' '9-8' '9-14' '9-16' '9-22' '9-23' '9-27' '9-31' '10-3' '10-11'\n",
      " '10-20' '10-23' '10-25' '10-26' '11-7' '11-8' '11-12' '11-15' '11-17'\n",
      " '11-20' '11-21' '11-25' '11-27' '11-31' '12-5' '12-9' '12-15' '12-19'\n",
      " '12-20' '12-21' '12-22' '13-3' '13-7' '13-8' '13-17' '13-18' '13-21'\n",
      " '13-30']\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 322)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"番泻苷4:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e70f5cc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "番泻苷5:1\n",
      "训练集样本编号:\n",
      "['12-31' '3-22' '7-29' '3-18' '10-24' '5-1' '12-6' '13-31' '4-4' '11-13'\n",
      " '4-27' '7-18' '5-3' '1-1' '4-22' '6-21' '11-4' '5-31' '12-24' '5-16'\n",
      " '13-9' '6-1' '8-21' '2-22' '5-23' '12-2' '7-7' '4-24' '5-18' '13-4' '4-1'\n",
      " '6-4' '1-26' '12-26' '4-8' '1-30' '2-28' '2-3' '3-4' '1-4' '3-23' '10-12'\n",
      " '6-14' '4-31' '13-1' '11-3' '6-15' '12-7' '13-29' '10-15' '8-24' '10-1'\n",
      " '5-30' '9-1' '12-1' '5-9' '1-22' '7-1' '12-29' '9-13' '8-11' '8-31'\n",
      " '12-30' '2-1' '4-11' '11-18' '9-29' '8-27' '2-14' '3-31' '5-11' '10-31'\n",
      " '3-14' '5-20' '13-25' '7-4' '7-27' '5-12' '13-6' '4-15' '2-31' '4-2'\n",
      " '10-19' '7-3' '8-8' '8-22' '11-30' '8-28' '8-9' '9-9' '2-2' '4-25' '5-2'\n",
      " '2-19' '10-2' '1-14' '9-24' '4-23' '8-23' '13-27' '1-11' '5-22' '7-13'\n",
      " '10-4' '7-15' '6-10' '4-20' '6-27' '12-10' '11-2' '1-24' '10-28' '5-25'\n",
      " '9-3' '12-25' '13-16' '5-13' '3-10' '6-31' '3-11' '5-4' '12-28' '12-11'\n",
      " '8-15' '3-26' '3-17' '13-14' '5-24' '8-25' '6-6' '9-19' '6-19' '13-22'\n",
      " '1-19' '11-24' '3-3' '1-2' '4-9' '7-12' '8-19' '8-29' '10-10' '13-10'\n",
      " '9-21' '10-14' '9-26' '8-16' '8-12' '5-26' '7-26' '4-26' '4-21' '13-23'\n",
      " '11-1' '13-26' '8-17' '9-10' '13-2' '8-2' '2-13' '2-11' '6-9' '1-6'\n",
      " '1-16' '7-28' '3-20' '10-7' '8-5' '12-27' '10-16' '9-2' '2-5' '5-8'\n",
      " '13-12' '12-17' '6-26' '10-29' '10-8' '1-28' '13-28' '13-20' '11-14'\n",
      " '1-17' '11-10' '12-8' '8-26' '4-13' '4-3' '6-3' '7-6' '8-3' '10-22'\n",
      " '3-25' '7-20' '4-29' '7-2' '3-28' '9-12' '12-12' '13-24' '12-16' '11-28'\n",
      " '9-15' '1-10' '7-19' '2-6' '8-30' '9-30' '7-23' '5-14' '6-13' '6-2'\n",
      " '3-16' '1-25' '7-16' '11-22' '9-28' '6-28' '2-24' '1-31' '10-17' '11-16'\n",
      " '8-20' '2-30' '3-19' '12-23' '9-17' '1-5' '9-18' '1-8' '7-11' '4-16'\n",
      " '5-7' '10-21' '2-29' '3-15' '12-14' '12-4' '4-14' '8-7' '11-6' '6-30'\n",
      " '6-17' '3-13' '1-3' '13-11' '3-30' '11-23' '7-17' '8-18' '2-12' '9-4'\n",
      " '7-5' '13-15' '10-27' '4-6' '3-8' '10-30' '5-19' '8-6' '12-13' '6-5'\n",
      " '5-6' '10-18' '4-12' '10-9' '10-6' '7-9' '9-7' '10-5' '5-17' '6-12'\n",
      " '4-30' '3-12' '12-3' '6-22' '7-22' '4-19' '1-12' '3-2' '4-28' '13-13'\n",
      " '8-14' '6-23' '1-20' '6-7' '11-29' '11-11' '7-24' '5-10' '8-1' '7-8'\n",
      " '7-31' '9-25' '7-25' '2-23' '11-9' '9-20' '5-27' '8-13' '6-18' '7-10'\n",
      " '9-11' '2-4' '3-9' '3-29' '3-21' '11-19' '6-11' '12-18' '1-23' '2-26'\n",
      " '13-5' '13-19' '3-1' '10-13' '6-29' '3-5' '11-26' '2-27' '11-5' '7-30'\n",
      " '10-26' '2-8' '12-22' '6-20' '11-20' '11-7' '7-14' '1-15' '4-17' '1-29'\n",
      " '8-10' '6-16' '5-15' '2-7']\n",
      "测试集样本编号:\n",
      "['1-7' '1-9' '1-13' '1-18' '1-21' '1-27' '2-9' '2-10' '2-15' '2-16' '2-17'\n",
      " '2-18' '2-20' '2-21' '2-25' '3-6' '3-7' '3-24' '3-27' '4-5' '4-7' '4-10'\n",
      " '4-18' '5-5' '5-21' '5-28' '5-29' '6-8' '6-24' '6-25' '7-21' '8-4' '9-5'\n",
      " '9-6' '9-8' '9-14' '9-16' '9-22' '9-23' '9-27' '9-31' '10-3' '10-11'\n",
      " '10-20' '10-23' '10-25' '11-8' '11-12' '11-15' '11-17' '11-21' '11-25'\n",
      " '11-27' '11-31' '12-5' '12-9' '12-15' '12-19' '12-20' '12-21' '13-3'\n",
      " '13-7' '13-8' '13-17' '13-18' '13-21' '13-30']\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 336) #\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"番泻苷5:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
